package com.task.controller;

import com.task.common.Cons;
import com.task.common.RandomValidateCode;
import com.task.common.ReturnResult;
import com.task.pojo.User;
import com.task.server.UserService;
import com.task.util.Assert;
import com.task.util.DateUtil;
import com.task.util.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author jiafujun
 * 2020年03月24日 12:58:00
 */
@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private UserService userService;

    /**
     * 登录
     *
     * @param req
     * @param res
     * @param code
     * @param account
     * @param pass
     * @return
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public ReturnResult login(HttpServletRequest req, HttpServletResponse res, String code, String account, String pass) {
        HttpSession session = req.getSession();
        String scode = session.getAttribute(RandomValidateCode.RANDOMCODEKEY).toString();

        Assert.notNull(code, "验证码不能为空");
        if (!scode.toLowerCase().equals(code.toLowerCase())) {
            return ReturnResult.build(Cons.RESULT_ERR, "验证码错误");
        }
        Assert.notNull(account, "账号不能为空");
        Assert.notNull(pass, "密码不能为空");

        User user = userService.getUser(account, null);
        if (user == null) {
            return ReturnResult.build(Cons.RESULT_ERR, "用户名或者密码错误");
        }
        String password = MD5Util.md5Password(account, pass.toUpperCase());
        if (!user.getPass().equals(password)) {
            return ReturnResult.build(Cons.RESULT_ERR, "用户名或者密码错误");
        }
        if (!user.getState().equals(0)) {
            return ReturnResult.build(Cons.RESULT_ERR, "该账户已经停止使用");
        }

        user.setPass(null);
        user.setName(null);
        user.setPhone(null);
        session.setAttribute("user", user);
        return ReturnResult.ok("登录成功", user);
    }


    /**
     * 注册
     *
     * @param req
     * @param user
     * @param code
     * @return
     */
    @RequestMapping(value = "/reg", method = RequestMethod.POST)
    public ReturnResult reg(HttpServletRequest req, User user, String code) {
        HttpSession session = req.getSession();
        String scode = session.getAttribute(RandomValidateCode.RANDOMCODEKEY).toString();

        Assert.notNull(code, "验证码不能为空");
        if (!scode.toLowerCase().equals(code.toLowerCase())) {
            return ReturnResult.build(Cons.RESULT_ERR, "验证码错误");
        }
        Assert.notNull(user.getAccount(), "账号不能为空");
        Assert.notNull(user.getPass(), "密码不能为空");
        Assert.notNull(user.getPhone(), "手机号码不能为空");
        Assert.notNull(user.getNickName(), "昵称不能为空");
        User rUser = userService.getUser(null, user.getPhone());
        if (rUser != null) {
            return ReturnResult.build(Cons.RESULT_ERR, "手机号已经被注册过了");
        }
        user.setPass(MD5Util.password(user.getAccount(), user.getPass()));
        user.setCreateTime(DateUtil.getStrTime());
        user.setState(0);
        userService.insert(user);
        return ReturnResult.ok("注册成功", user);
    }

    /**
     * 退出登录
     *
     * @param req
     * @return
     */
    @RequestMapping(value = "/logout", method = {RequestMethod.POST, RequestMethod.GET})
    public void logout(HttpServletRequest req,HttpServletResponse res) {
        HttpSession session = req.getSession();
        session.setAttribute("user", null);
        try {
            res.sendRedirect(req.getContextPath() + "/page/login");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
